package com.ctshk.sapp.order.goods.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.goods.dto.OrderDetailDTO;
import com.ctshk.rpc.order.goods.dto.OrderPageListDTO;
import com.ctshk.rpc.order.goods.dto.QueryQuotaDTO;
import com.ctshk.rpc.order.goods.req.*;
import com.ctshk.rpc.order.goods.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 黄利斌
 * @since 2021-04-19
 */
@Api(value="商品訂購", tags="商品訂購")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @DubboReference
    private IOrderService orderService;


    @ApiOperation(value = "B端-商品-支付-查询挂账额度", notes = "B端-商品-支付-查询挂账额度")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "挂账额度-查询额度", operDesc = "挂账额度-查询额度")
    @PostMapping("/queryQuota")
    public Result<QueryQuotaDTO> queryQuota(@RequestBody @Valid QueryQuotaReq req) {
        return orderService.queryQuota(req);
    }

    @ApiOperation(value = "B端-测试-收款-驳回", notes = "B端-测试-收款-驳回")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "B端-测试-收款-驳回", operDesc = "B端-测试-收款-驳回")
    @PostMapping("/rejectPayment")
    public Result rejectPayment(@RequestParam String collectionNumber,@RequestParam Long userId,@RequestParam String msg) {
        return orderService.rejectPayment(collectionNumber, userId, msg);
//        return Result.success();
    }

    @ApiOperation(value = "B端-新增-門店-訂單", notes = "B端-新增-門店-訂單")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "B端-新增-門店-訂單", operDesc = "B端-新增-門店-訂單")
    @PostMapping("/addOrder")
    public Result<Long> addOrder(@RequestBody @Valid OrderAddReq obj) throws Exception {
        Long orderId = orderService.addOrder(obj, getCurrentUser(getRequest()));
        return Result.success(orderId);
    }


    @ApiOperation(value = "B端-商品-訂單-支付", notes = "B端-商品-訂單-支付")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "B端-商品-訂單-支付", operDesc = "B端-商品-訂單-支付")
    @PostMapping("/payment/{oderId}")
    public Result payment(@PathVariable("oderId") Long oderId, @Valid @RequestBody List<OrderCommodityCollectionReq> orderCommodityCollectionReqList) throws Exception {
        orderService.payment(oderId, orderCommodityCollectionReqList, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "B端-商品-取消-訂單", notes = "B端-商品-取消-訂單")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "B端-商品-取消-訂單", operDesc = "B端-商品-取消-訂單")
    @PostMapping("/cancelOrder")
    public Result cancelOrder(@RequestBody @Valid OrderCommodityRefundReq2 refundReqs) throws Exception {
        orderService.cancelOrder(refundReqs, getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "B端-商品-備注-訂單", notes = "B端-商品-備注-訂單")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "B端-商品-備注-訂單", operDesc = "B端-商品-備注-訂單")
    @PostMapping("/remarkOrder")
    public Result remarkOrder(@RequestBody @Valid OrderAddReq addReq) throws Exception {
        orderService.remarkOrder(addReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "B端-商品-訂單-分頁列表", notes = "B端-商品-訂單-分頁列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "B端-商品-訂單-分頁列表", operDesc = "B端-商品-訂單-分頁列表")
    @PostMapping("/orderPageList")
    public Result<PageResponse<OrderPageListDTO>> orderPageList(@RequestBody @Valid OrderPageListReq req ){
        return Result.success(orderService.orderPageList(req, getCurrentUser(getRequest())));
    }

    @ApiOperation(value = "B端-根据-商品ID-查询訂單明细", notes = "B端-根据-商品ID-查询訂單明细")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "B端-根据-商品ID-查询訂單明细", operDesc = "B端-根据-商品ID-查询訂單明细")
    @GetMapping("/getOrderDetailed/{orderId}/{type}")
    public Result<OrderDetailDTO> getOrderDetailed(@PathVariable("orderId") Long orderId, @PathVariable("type") Integer type){
        OrderDetailDTO resultData = orderService.getOrderDetailed(orderId, type, getCurrentUser(getRequest()));
        return Result.success(resultData);
    }

    @ApiOperation(value = "B端-修改-商品-收货人信息", notes = "B端-修改-商品-收货人信息")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "B端-修改-商品-收货人信息", operDesc = "B端-修改-商品-收货人信息")
    @PostMapping("/updateOrderConsigneeInfo")
    public Result updateOrderConsigneeInfo(@RequestBody @Valid OrderConsigneeInfoReq req){
        orderService.updateOrderConsigneeInfo(req, getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "B端-商品-订单-发货", notes = "B端-商品-订单-发货")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "B端-商品-订单-发货", operDesc = "B端-商品-订单-发货")
    @PostMapping("/upOrderCommodityDeliverGoods")
    public Result upOrderCommodityDeliverGoods(@RequestBody @Valid OrderCommodityDeliverGoodsReq req){
        orderService.upOrderCommodityDeliverGoods(req, getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "B端-商品-取消订单审批-状态更新", notes = "B端-商品-取消订单审批-状态更新")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "B端-商品-取消订单审批-状态更新", operDesc = "B端-商品-取消订单审批-状态更新")
    @PostMapping("/cancelOrderApprovalProcess")
    public Result cancelOrderApprovalProcess(@RequestBody OrderCancelApprovalProcessReq obj) {
        orderService.cancelOrderApprovalProcess(obj,getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "B端-商品-删除退款记录-只删除已经拒绝的退款记录", notes = "B端-商品-删除退款记录-只删除已经拒绝的退款记录")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.DELETE, operTitle = "B端-商品-删除退款记录-只删除已经拒绝的退款记录", operDesc = "B端-商品-删除退款记录-只删除已经拒绝的退款记录")
    @GetMapping("/deleteApprovalRejected/{id}")
    public Result deleteApprovalRejected(@PathVariable("id") Long id) {
        orderService.deleteApprovalRejected(id,getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "B端-商品-操作退款-退款确认", notes = "B端-商品-操作退款-退款确认")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "B端-商品-操作退款-退款确认", operDesc = "B端-商品-操作退款-退款确认")
    @GetMapping("/updateRefundStatus/{id}")
    public Result updateRefundStatus(@PathVariable("id") Long id) {
        orderService.updateRefundStatus(id,getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "B端-商品-预订中心-商品订单-支付订单", notes = "B端-商品-预订中心-商品订单-支付订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "B端-商品-预订中心-商品订单-支付订单", operDesc = "B端-商品-预订中心-商品订单-支付订单")
    @PostMapping(value = "/pay/{id}")
    public Result payOrder(@PathVariable("id") Long id,@RequestBody OrderCommodityPayReq req){
        TokenUser currentUser = getCurrentUser(getRequest());
        return orderService.payOrder(req, getCurrentUser(getRequest()));
    }
}
